package com.bcxj.servicedriveruser.servcie;

import com.bcxj.internalcommon.dto.ResponseResult;
import com.bcxj.internalcommon.entity.Car;
import com.bcxj.internalcommon.response.TerminalResponse;
import com.bcxj.internalcommon.response.TrackResponse;
import com.bcxj.servicedriveruser.mapper.CarMapper;
import com.bcxj.servicedriveruser.remote.ServiceMapClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * ClassName: CarService
 * Package: com.bcxj.servicedriveruser.servcie
 * Description:
 *
 * @Author BCXJ
 * @Create 2024/10/20 10:42
 * @Version 1.0
 * @Since 1.0
 */
@Service
public class CarService {

    @Autowired
    private CarMapper carMapper;
    @Autowired
    private ServiceMapClient serviceMapClient;

    /**
     * 增加车辆
     * @param car
     * @return
     */
    public ResponseResult addCar(Car car) {
        LocalDateTime now = LocalDateTime.now();
        car.setGmtCreate(now);
        car.setGmtCreate(now);
        carMapper.insert(car);

        // 车辆绑定tid
        ResponseResult<TerminalResponse> terminalResponseResponseResult = serviceMapClient.addTerminal(car.getVehicleNo(), car.getId().toString());
        String tid = terminalResponseResponseResult.getData().getTid();
        car.setTid(tid);
        ResponseResult<TrackResponse> trackResponseResponseResult = serviceMapClient.addTrack(tid);
        car.setTrid(trackResponseResponseResult.getData().getTird());
        car.setTrname(trackResponseResponseResult.getData().getTrname());
        carMapper.updateById(car);

        return ResponseResult.success();
    }


    /**
     * 获取car的具体的信息
     * @param carId
     * @return
     */
    public ResponseResult<Car> getCar(String carId) {
        Car car = carMapper.selectById(carId);
        return ResponseResult.success(car);
    }
}
